// 初始化页面
document.addEventListener('DOMContentLoaded', function() {
    // 初始化轮播图
    initHeroSlider();
    
    // 初始化选项卡
    initTabs();
    
    // 初始化导航菜单切换
    initMobileMenu();
    
    // 初始化返回顶部按钮
    initBackToTop();
});

// 轮播图功能
function initHeroSlider() {
    const slider = document.querySelector('.hero-slider');
    const slides = document.querySelectorAll('.slide');
    const dotsContainer = document.querySelector('.slider-dots');
    let currentSlide = 0;
    let slideInterval = setInterval(nextSlide, 5000);

    // 创建指示点
    slides.forEach((_, index) => {
        const dot = document.createElement('span');
        dot.className = index === 0 ? 'active' : '';
        dot.addEventListener('click', () => goToSlide(index));
        dotsContainer.appendChild(dot);
    });

    function nextSlide() {
        currentSlide = (currentSlide + 1) % slides.length;
        updateSlider();
    }

    function prevSlide() {
        currentSlide = (currentSlide - 1 + slides.length) % slides.length;
        updateSlider();
    }

    function goToSlide(index) {
        currentSlide = index;
        updateSlider();
    }

    function updateSlider() {
        clearInterval(slideInterval);
        slideInterval = setInterval(nextSlide, 5000);
        
        slides.forEach((slide, index) => {
            slide.classList.toggle('active', index === currentSlide);
        });
        
        const dots = document.querySelectorAll('.slider-dots span');
        dots.forEach((dot, index) => {
            dot.classList.toggle('active', index === currentSlide);
        });
    }
}

// 选项卡功能
function initTabs() {
    const tabButtons = document.querySelectorAll('.tab-btn');
    const tabContents = document.querySelectorAll('.tab-content');

    tabButtons.forEach(button => {
        button.addEventListener('click', () => {
            const targetId = button.dataset.tab;

            // 移除 active 类并隐藏所有内容
            tabButtons.forEach(btn => btn.classList.remove('active'));
            tabContents.forEach(content => {
                content.classList.remove('active');
                content.classList.add('hidden');
            });

            // 显示目标内容
            const targetContent = document.getElementById(targetId);
            targetContent.classList.remove('hidden');
            setTimeout(() => {
                targetContent.classList.add('active');
            }, 50);

            // 设置当前按钮为 active
            button.classList.add('active');
        });
    });
}

// 移动端菜单功能
function initMobileMenu() {
    const menuToggle = document.querySelector('.menu-toggle');
    const navMenu = document.querySelector('.nav-menu');

    if (menuToggle && navMenu) {
        menuToggle.addEventListener('click', () => {
            navMenu.classList.toggle('show');
        });
    }
}

// 返回顶部按钮功能
function initBackToTop() {
    const backToTopBtn = document.querySelector('.back-to-top');

    window.addEventListener('scroll', () => {
        if (window.pageYOffset > 300) {
            backToTopBtn.style.display = 'flex';
        } else {
            backToTopBtn.style.display = 'none';
        }
    });

    backToTopBtn.addEventListener('click', (e) => {
        e.preventDefault();
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
    });
}

// 地图相关功能
function resetMap() {
    // 实现地图重置逻辑
    console.log('Resetting map view...');
}

function locateUser() {
    // 实现用户定位逻辑
    console.log('Locating user...');
}